package com.jues.videobanner;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Android Studio .
 * 作者：zhong
 * 日期：2019/3/7
 * 邮箱：15206394364@163.com
 * 介绍：
 * 修订：====================
 */
public class BannerViewAdapter extends PagerAdapter {
    private List<View> listBean;
    private View.OnClickListener mOnClickListener;

    public BannerViewAdapter(List<View> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.listBean = list;
    }

    public void setDataList(List<View> list) {
        if (list != null && list.size() > 0) {
            this.listBean = list;
        }
    }

    public void setListener(View.OnClickListener listener){
        this.mOnClickListener = listener;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        try {
            View view = listBean.get(position);
            if (null != mOnClickListener) view.setOnClickListener(mOnClickListener);
            container.addView(view);
            return view;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return listBean.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
